package org.devio.`as`.proj.main.biz.account

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.activity_register.action_back
import kotlinx.android.synthetic.main.activity_register.input_item_username
import org.devio.`as`.proj.common.ui.component.HiBaseActivity
import org.devio.`as`.proj.main.R
import org.devio.`as`.proj.main.http.ApiFactory
import org.devio.`as`.proj.main.http.api.AccountApi
import org.devio.hi.library.restful.HiCallback
import org.devio.hi.library.restful.HiResponse
import org.devio.hi.library.util.HiStatusBar

@Route(path = "/account/registration")
class RegistrationActivity : HiBaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        HiStatusBar.setStatusBar(this, true, translucent = false)

        setContentView(R.layout.activity_register)

        action_back.setOnClickListener { onBackPressed() }

        action_submit.setOnClickListener { submit() }
    }

    private fun submit() {
        val orderId = input_item_orderId.getEditText().text.toString()
        val username = input_item_username.getEditText().text.toString()
        val pwd = input_item_pwd.getEditText().text.toString()
        val pwdSec = input_item_pwd_check.getEditText().text.toString()

        var errorMsg = ""
        val suffix = "不能为空"

        if (TextUtils.isEmpty(username) && TextUtils.isEmpty(username)) {
            errorMsg = "用户名 $suffix"
        }
        if (TextUtils.isEmpty(orderId) && TextUtils.isEmpty(orderId)) {
            errorMsg = "验证码 $suffix"
        }

        if (TextUtils.isEmpty(pwd) &&TextUtils.isEmpty(pwd)) {
            errorMsg = "密码 $suffix"
        }

        if (TextUtils.isEmpty(pwdSec) &&TextUtils.isEmpty(pwdSec)) {
            errorMsg = "确认密码 $suffix"
        }

        if (TextUtils.isEmpty(pwdSec) && pwd != pwdSec) {
            errorMsg = "两次密码输入不一致，请确认后重试"
        }

        if (!TextUtils.isEmpty(errorMsg)) {
            showToast(errorMsg)
            return
        }

        //注册成功
        var intent = Intent()
        intent.putExtra("username", username)
        setResult(Activity.RESULT_OK, intent)
        finish()
        return;
        ApiFactory.create(AccountApi::class.java).register(username, pwd, "moocId", orderId)
            .enqueue(object : HiCallback<String> {
                override fun onSuccess(response: HiResponse<String>) {
                    if (response.code == HiResponse.SUCCESS) {
                        //注册成功
                        var intent = Intent()
                        intent.putExtra("username", username)
                        setResult(Activity.RESULT_OK, intent)
                        finish()
                    } else {
                        showToast(response.msg)
                    }
                }

                override fun onFailed(throwable: Throwable) {
                    showToast(throwable.message)
                }
            })
    }
}